package com.li.shop.controller;

import com.li.shop.domain.Coupon;
import com.li.shop.service.CouponService;
import com.li.shop.utils.SearchCouponResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

//杨煦瑞做
@Controller
@RequestMapping("/coupon")

public class CouponController {

    @Autowired
    private CouponService couponService;

    //王志文
    @RequestMapping(value = "/searchcoupon", method = RequestMethod.GET)
    public String couponPage(Model model, Integer pageSize, Integer pageNum) {
        //TODO: Add coupon search result
        //普通显示
        // List<Coupon> Coupons = couponService.listCoupon( pageSize,pageNum);
        //model.addAttribute("coupons", coupons);
        //return "coupon-list-bak";//普通页面
        //return "coupon-list";//adminLTE页面
        return "/back/coupon-list";
    }

    //分页显示
    @GetMapping("/api/searchcoupon")
    @ResponseBody//需要返回json数据
    public SearchCouponResponse searchCoupon(Integer pageNum, Integer pageSize, String keyword) {
        return couponService.listCoupon(pageSize, pageNum, keyword);
    }

    @RequestMapping(value = "/addcoupon", method = RequestMethod.GET)
    public String addCouponPage(Model model) {
        model.addAttribute("coupon", new Coupon());
        return "/back/coupon-add";
    }

    @RequestMapping(value = "/addcouponb", method = RequestMethod.GET)
    public String addCouponPageb(Model model) {
        model.addAttribute("coupon", new Coupon());
        return "/back/coupon-addb";
    }


    @GetMapping("/editcoupon")
    public String editCouponPage(@RequestParam("id") String id, Model model) {
        Coupon coupon = couponService.findCouponByPrimaryKey(id);
        model.addAttribute("coupon", coupon);
        return "/back/coupon-add";
    }

    @RequestMapping(value = "/addcoupon", method = RequestMethod.POST)
    public String saveCoupon(Coupon coupon) {
        int updated = couponService.saveCoupon(coupon);
        return "redirect:/coupon/searchcoupon";
    }

    @RequestMapping(value = "/deletecoupon", method = RequestMethod.GET)
    //@ResponseBody//需要返回json数据 @RequestParam("id") @PathVariable("id")
    public String deleteCoupon(@RequestParam("id") String id) {
        //var mymessage = confirm("确认删除嘛？");
        //if (mymessage == true) {
        int deleted = couponService.deleteCouponByPrimaryKey(id);

        // return deleted > 0;
        //return "redirect:/backadmincoupon/searchcoupon";//get请求不能用重定向
        return "/back/coupon-list";
        //return "redirect:/backadmincoupon/deletecoupon";
    }
}
